/* Copyright 2013-2015 www.snakerflow.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.snaker.engine.helper;

import org.snaker.engine.SnakerException;
import cn.hutool.core.util.StrUtil;

/**
 * 字符串处理帮助类
 * 
 * @author yuqs
 * @since 1.0
 */
public class StringHelper {
    /**
     * 构造排序条件
     * 
     * @param order
     *            排序方向
     * @param orderby
     *            排序字段
     * @return 组装好的排序sql
     */
    public static String buildPageOrder(String order, String orderby) {
        if (StrUtil.isBlank(orderby) || StrUtil.isBlank(order))
            return "";
        String[] orderByArray = StrUtil.splitToArray(orderby, ',');
        String[] orderArray = StrUtil.splitToArray(order, ',');
        if (orderArray.length != orderByArray.length)
            throw new SnakerException("分页多重排序参数中,排序字段与排序方向的个数不相等");
        StringBuilder orderStr = new StringBuilder(30);
        orderStr.append(" order by ");

        for (int i = 0; i < orderByArray.length; i++) {
            orderStr.append(orderByArray[i]).append(" ").append(orderArray[i]).append(" ,");
        }
        orderStr.deleteCharAt(orderStr.length() - 1);
        return orderStr.toString();
    }
}
